﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace VisionCore
{
    public static class FilePathHelper
    {
        private static string GetShareFileDir(string deviceName)
        {
            string path = string.Format(@"{0}\{1}", ParameterHelper.SysParam.FilePath.ShareFileDir, deviceName);

            CreateDir(path);
            return path;
        }

        /// <summary>
        /// 获取Train路径
        /// </summary>
        /// <param name="deviceName"></param>
        /// <returns></returns>
        public static string GetTrain_Dir(string deviceName)
        {
            if (string.IsNullOrEmpty(deviceName))
            {
                return "";
            }

            string path = string.Format(@"{0}\{1}", GetShareFileDir(deviceName), ParameterHelper.SysParam.FilePath.WaferTrainFileName);

            CreateDir(path);
            return path;
        }

        /// <summary>
        /// 获取MapData路径(本地)
        /// </summary>
        /// <param name="deviceName">工单名称</param>
        /// <param name="deviceType">false:共用Map true:单独Map</param>
        /// <param name="lotNum"></param>
        /// <returns></returns>
        public static string GetWaferMap_Dir(string deviceName, bool deviceType, string lotNum = "")
        {
            if (string.IsNullOrEmpty(deviceName))
            {
                return "";
            }

            if (deviceType == true && string.IsNullOrEmpty(lotNum))
            {
                return "";
            }

            string path = "";
            if (deviceType == false)
            {
                path = string.Format(@"{0}\{1}", GetShareFileDir(deviceName), ParameterHelper.SysParam.FilePath.WaferMapFileName);
            }
            else
            {
                path = string.Format(@"{0}\{1}\lots\{2}", GetShareFileDir(deviceName), lotNum, ParameterHelper.SysParam.FilePath.WaferMapFileName);
            }

            CreateDir(path);
            return path;
        }

        /// <summary>
        /// 获取MapData路径(客服端)
        /// </summary>
        /// <param name="lotNum"></param>
        /// <returns></returns>
        public static string GetWaferMap_Dir(string lotNum = "")
        {
            if (string.IsNullOrEmpty(lotNum)) return "";

            string path = "";
            path = string.Format(@"{0}\{1}", ParameterHelper.SysParam.FilePath.MapDataFileDir_User, lotNum);

            return path;
        }

        private static string GetResult_Dir(string deviceName, string lotNum, string waferID)
        {
            if (string.IsNullOrEmpty(deviceName) || string.IsNullOrEmpty(lotNum) || string.IsNullOrEmpty(waferID))
            {
                return "";
            }

            string path = string.Format(@"{0}\lots\{1}\wafers\{2}", GetShareFileDir(deviceName), lotNum, waferID);

            CreateDir(path);
            return path;
        }

        /// <summary>
        /// 获取复判数据路径
        /// </summary>
        /// <param name="deviceName"></param>
        /// <param name="lotNum"></param>
        /// <param name="waferID"></param>
        /// <returns></returns>
        public static string GetReInsp_Dir(string deviceName, string lotNum, string waferID)
        {
            if (string.IsNullOrEmpty(deviceName) || string.IsNullOrEmpty(lotNum) || string.IsNullOrEmpty(waferID))
            {
                return "";
            }

            string path = string.Format(@"{0}\{1}", GetResult_Dir(deviceName, lotNum, waferID)
                , ParameterHelper.SysParam.FilePath.ReInspectionFileName);

            CreateDir(path);
            return path;
        }

        /// <summary>
        /// 获取复判图片保存路径
        /// </summary>
        /// <param name="deviceName"></param>
        /// <param name="lotNum"></param>
        /// <param name="waferID"></param>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string GetReInspImg_Dir(string deviceName, string lotNum, string waferID, DateTime dateTime)
        {
            if (string.IsNullOrEmpty(deviceName) || string.IsNullOrEmpty(lotNum) || string.IsNullOrEmpty(waferID))
            {
                return "";
            }
            string tFolder = string.Format("defects {0}", dateTime.ToString("yyyy-MM-dd HHmmss"));
            string path = string.Format(@"{0}\{1}\{2}", GetResult_Dir(deviceName, lotNum, waferID), tFolder, ParameterHelper.SysParam.FilePath.ReInspectionFileName);

            CreateDir(path);
            return path;
        }

        /// <summary>
        /// 复判结果路径
        /// </summary>
        /// <param name="deviceName"></param>
        /// <param name="lotNum"></param>
        /// <param name="waferID"></param>
        /// <returns></returns>
        public static string GetRebin_Dir(string deviceName, string lotNum, string waferID)
        {
            if (string.IsNullOrEmpty(deviceName) || string.IsNullOrEmpty(lotNum) || string.IsNullOrEmpty(waferID))
            {
                return "";
            }

            string path = string.Format(@"{0}\{1}\{2}", GetResult_Dir(deviceName, lotNum, waferID), ParameterHelper.SysParam.FilePath.ReInspectionFileName, ParameterHelper.SysParam.FilePath.RebinDataFileName);

            //CreateDir(path);
            return path;
        }

        public static string GetUnloadMap_Dir(string deviceName, string lotNum, string waferID)
        {
            if (string.IsNullOrEmpty(deviceName) || string.IsNullOrEmpty(lotNum) || string.IsNullOrEmpty(waferID))
            {
                return "";
            }

            string path = string.Format(@"{0}\{1}\{2}", GetResult_Dir(deviceName, lotNum, waferID), ParameterHelper.SysParam.FilePath.ReInspectionFileName, ParameterHelper.SysParam.FilePath.UnloadMapDataFileName);

            CreateDir(path);
            return path;
        }

        private static void CreateDir(string path)
        {
            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }
        }

        /// <summary>
        /// 复制文件夹中文件到另一个文件夹
        /// </summary>
        /// <param name="srcPath">源文件夹</param>
        /// <param name="aimPath">目标文件夹</param>
        public static void CopyDir(string srcPath, string aimPath)
        {
            if (string.IsNullOrEmpty(aimPath)) return;

            try
            {
                // 检查目标目录是否以目录分割字符结束如果不是则添加
                if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
                {
                    aimPath += System.IO.Path.DirectorySeparatorChar;
                }
                // 判断目标目录是否存在如果不存在则新建
                if (!System.IO.Directory.Exists(aimPath))
                {
                    System.IO.Directory.CreateDirectory(aimPath);
                }
                // 得到源目录的文件列表，该里面是包含文件以及目录路径的一个数组
                // 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
                // string[] fileList = Directory.GetFiles（srcPath）；
                string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath);
                // 遍历所有的文件和目录
                foreach (string file in fileList)
                {
                    // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                    if (System.IO.Directory.Exists(file))
                    {
                        CopyDir(file, aimPath + System.IO.Path.GetFileName(file));
                    }
                    // 否则直接Copy文件
                    else
                    {
                        System.IO.File.Copy(file, aimPath + System.IO.Path.GetFileName(file), true);
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
    }
}
